package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.MemberInfo;
import com.cls.business.service.IMemberInfoService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 成员信息表 Controller
 *
 * @author wandering
 * @date 2020-11-12 18:07:06
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class MemberInfoController extends BaseController {

    private final IMemberInfoService memberInfoService;

    /**
    * 成员信息表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberInfo")
    public String memberInfoIndex(){
        return MyUtil.view("business/memberInfo/memberInfo");
    }

    /**
    * 成员信息表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberInfo/memberInfoAdd")
    public String memberInfoAdd(Model model){
        return MyUtil.view("business/memberInfo/memberInfoAdd");
    }

    /**
    * 成员信息表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberInfo/memberInfoUpdate/{memberInfoId}")
    public String memberInfoUpdate(@PathVariable Long memberInfoId, Model model){
        MemberInfo memberInfo = memberInfoService.getById(memberInfoId);
        model.addAttribute("memberInfo",memberInfo);
        return MyUtil.view("business/memberInfo/memberInfoUpdate");
    }



    /**
    * 成员信息表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberInfo/memberInfoDetail/{memberInfoId}")
    public String memberInfoDetail(@PathVariable Long memberInfoId, Model model){
        MemberInfo memberInfo = memberInfoService.getById(memberInfoId);
        model.addAttribute("memberInfo",memberInfo);
        return MyUtil.view("business/memberInfo/memberInfoDetail");
    }

    @GetMapping("memberInfo")
    @ResponseBody
    @RequiresPermissions("memberInfo:list")
    public ResponseVO getAllMemberInfos(MemberInfo memberInfo) {
        return ResponseVO.success(memberInfoService.findMemberInfos(memberInfo));
    }

    @GetMapping("{declareId}/memberInfo/list")
    @ResponseBody
//    @RequiresPermissions("memberInfo:list")
    public ResponseVO memberInfoList(QueryRequest request, @PathVariable long declareId) {
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setDeclareId(declareId);
        PageInfo<MemberInfo> pageInfo =  this.memberInfoService.findMemberInfos(request, memberInfo);
        return ResponseVO.success(pageInfo);
    }
    @GetMapping("{declareId}/memberInfo/listAll")
    @ResponseBody
    public ResponseVO memberInfoLists(QueryRequest request, @PathVariable long declareId) {
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setDeclareId(declareId);
        List<MemberInfo> lists =  this.memberInfoService.findMemberInfos(memberInfo);
        return ResponseVO.success(lists);
    }


    @ControllerEndpoint(operation = "新增MemberInfo", exceptionMessage = "新增MemberInfo失败")
    @PostMapping("memberInfo")
    @ResponseBody
    @RequiresPermissions("memberInfo:add")
    public ResponseVO addMemberInfo(@Valid MemberInfo memberInfo) {
        this.memberInfoService.createMemberInfo(memberInfo);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除MemberInfo", exceptionMessage = "删除MemberInfo失败")
    @PostMapping("memberInfo/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("memberInfo:delete")
    public ResponseVO deleteMemberInfos(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.memberInfoService.deleteMemberInfos(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个MemberInfo", exceptionMessage = "删除MemberInfo失败")
    @PostMapping("memberInfo/delete/{memberInfoId}")
    @ResponseBody
    @RequiresPermissions("memberInfo:delete")
    public ResponseVO deleteMemberInfo(@PathVariable Long memberInfoId) {
        this.memberInfoService.removeById(memberInfoId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改MemberInfo", exceptionMessage = "修改MemberInfo失败")
    @PostMapping("memberInfo/update")
    @ResponseBody
    @RequiresPermissions("memberInfo:update")
    public ResponseVO updateMemberInfo(MemberInfo memberInfo) {
        this.memberInfoService.updateMemberInfo(memberInfo);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改MemberInfo", exceptionMessage = "导出Excel失败")
    @PostMapping("memberInfo/excel")
    @ResponseBody
    @RequiresPermissions("memberInfo:export")
    public void export(QueryRequest queryRequest, MemberInfo memberInfo, HttpServletResponse response) {
        List<MemberInfo> memberInfos = this.memberInfoService.findMemberInfos(queryRequest, memberInfo).getList();
        ExcelKit.$Export(MemberInfo.class, response).downXlsx(memberInfos, false);
    }
}
